package com.ptszyxx.popose.module.base.user.vm;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.ptszyxx.popose.R;
import com.ptszyxx.popose.common.oos.YOosUtil;
import com.ptszyxx.popose.common.utils.YActivityUtil;
import com.ysg.base.BaseViewModel;
import com.ysg.binding.command.BindingCommand;
import com.ysg.bus.event.SingleLiveEvent;
import com.ysg.http.BaseResponse;
import com.ysg.http.HttpUtils;
import com.ysg.http.callback.OnSuccessResult;
import com.ysg.http.data.CommonRepository;
import com.ysg.utils.YSPUtils;
import com.ysg.utils.YStringUtil;
import com.ysg.utils.YToastUtil;
import com.ysg.utils.YUserUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserAvatarVM extends BaseViewModel<CommonRepository> {
    public ObservableField<String> avatarPath = new ObservableField<>();

    public UserAvatarVM(@NonNull Application application, CommonRepository repository) {
        super(application, repository);
    }

    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        public SingleLiveEvent onPicEvent = new SingleLiveEvent<>();
    }


    public BindingCommand onSubmitCommand = new BindingCommand(() -> requestUploadPic());
    public BindingCommand onPicCommand = new BindingCommand(() -> uc.onPicEvent.call());
    public BindingCommand onJumpCommand = new BindingCommand(() -> onJumpTag());

    public void requestOosToken(){
        YOosUtil.getInstance().getOosToken(model, this);
    }

    private void requestUploadPic() {
        if (!check())return;
        String userId = YSPUtils.getInstance().getUserId();
        YOosUtil.getInstance().upload(avatarPath.get(), userId, true,  new YOosUtil.OnOosListener() {
            @Override
            public void onSuccess(String url) {
                requestUserUpdate(url);
            }

            @Override
            public void onSuccess(List<String> list) {

            }

            @Override
            public void onError() {
            }
        });
    }

    private void requestUserUpdate(String url) {
        if (!check())return;
        Map<String, String> map = new HashMap<>();
        map.put("pic", url);
        HttpUtils.getInstance()
                .data(model.userEditAvatar(map), this,
                        new OnSuccessResult() {
                            @Override
                            public void onSuccessResult(BaseResponse response) {
                                YUserUtil.saveAvatar(url);
                                onJumpTag();
                            }
                        });
    }


    private boolean check(){
        if (YStringUtil.isEmpty(avatarPath.get())){
            YToastUtil.showShort(R.string.avatar_hint);
            return false;
        }
        return true;
    }

    private void onJumpTag() {
        YActivityUtil.getInstance().jumpUserTag(this);
    }

}
